/*
 * @Author: huang yuanfa
 * @Date: 2022-10-19 22:39:03
 * @LastEditTime: 2022-11-17 09:16:10
 * @LastEditors: huang yuanfa
 * Copyright 2022 huang yuanfa All Rights Reserved.
 * @Gitee: https://gitee.com/huangyuanfa
 * @Github: https://github.com/hyf888
 * @FilePath: /src/Utils/UtilsDefine.h
 * @Description: file content
 */

#ifndef __UTILSDEFINE_H__
#define __UTILSDEFINE_H__

#include <mutex>
#include <string>
#include "HDefine.h"

HTHREADPOOL_NAMESPACE_BEGIN

#ifdef _ENABLE_LIKELY_
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely
#define unlikely
#endif

#define ASSERT_NOT_NULL(ptr)                                                                                           \
    if(unlikely(nullptr == (ptr))) {                                                                                   \
        return HStatus("ptr is nullptr");                                                                              \
    }

#define ASSERT_NOT_NULL_RETURN_NULL(ptr)                                                                               \
    if(unlikely(nullptr == (ptr))) {                                                                                   \
        return nullptr;                                                                                                \
    }

    static std::mutex g_check_status_mtx;
#define FUNCTION_CHECK_STATUS                                                                                          \
    if(unlikely(!status.isOK())) {                                                                                     \
        std::lock_guard<std::mutex> lock{g_check_status_mtx};                                                          \
        Echo("%s | %s | line = [%d], errorCode = [%d], errorInfo = [%s].",                                             \
             __FILE__,                                                                                                 \
             __FUNCTION__,                                                                                             \
             __LINE__,                                                                                                 \
             status.getCode(),                                                                                         \
             status.getInfo().c_str());                                                                                \
        return status;                                                                                                 \
    }

#define DELETE_PTR(ptr)                                                                                                \
    if(unlikely((ptr) != nullptr)) {                                                                                   \
        delete(ptr);                                                                                                   \
        (ptr) = nullptr;                                                                                               \
    }

#define ASSERT_INIT(isInit)                                                                                            \
    if(unlikely((isInit) != m_isInit)) {                                                                               \
        return HStatus("init status is not suitable");                                                                 \
    }

#define ASSERT_INIT_RETURN_NULL(isInit)                                                                                \
    if(unlikely((isInit) != is_init_)) {                                                                               \
        return nullptr;                                                                                                \
    }

#define SLEEP_MILLISECOND(ms) std::this_thread::sleep_for(std::chrono::milliseconds(ms));

#define SLEEP_SECOND(s) std::this_thread::sleep_for(std::chrono::seconds(s));

HTHREADPOOL_NAMESPACE_END

#endif
